<template>
  <div class="mod-menu">
    <el-form :inline="true" :model="pamars">
      <el-form-item label="活动名称">
        <el-input v-model="pamars.name" placeholder="活动名称" clearable></el-input>
      </el-form-item>
      <el-form-item label="活动状态">
        <el-select v-model="pamars.active_state" clearable placeholder="请选择">
          <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="dataList" row-key="Id" border style="width: 100%; ">
      <el-table-column prop="name" align="center" label="活动名称">
      </el-table-column>
      <el-table-column prop="goods_num" align="center" label="活动商品">
      </el-table-column>
      <el-table-column align="center" label="活动时间">
        <template slot-scope="scope">
          <div>开始时间： {{scope.row.start_time}}</div>
          <div>结束时间：{{scope.row.end_time}}</div>
        </template>
      </el-table-column>
      <el-table-column prop="active_state" align="center" :formatter="activeState" label="活动状态">
      </el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)" v-if="scope.row.active_state==0">修改</el-button>
          <el-button type="text" size="small" @click="goProduct(scope.row.id)"> 管理商品</el-button>

          <el-button type="text" size="small" class="c-error" @click="deleteHandle(scope.row.id)" v-if="scope.row.active_state!=1">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pamars.page" :page-sizes="[10, 20, 50, 100]" :page-size="pamars.limit" :total="totalPage" layout="total, sizes, prev, pager, next, jumper"></el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <group-booking-add-update v-if="addOrUpdateVisible" ref="groupBookingAddUpdate" @updataList='updataList'></group-booking-add-update>

  </div>
</template>

<script>
import groupBookingAddUpdate from './group-booking-add-update'
import seviceApi from '@/api/serviceApi.js'

export default {
  data () {
    return {

      pamars: {
        page: 1,
        limit: 10,
        active_state: '',
        name: ''
      },
      showGroupMember: false,
      totalPage: 0,
      dataList: [],
      options: [
        {
          label: '未开始',
          value: 0
        }, {
          label: '进行中',
          value: 1
        }, {
          label: '已结束',
          value: 2
        }
      ],
      dataListLoading: false,
      addOrUpdateVisible: false
    }
  },
  components: {
    groupBookingAddUpdate

  },
  mounted () {
    this.getList()
  },
  methods: {
    getMember (id) {
      this.showGroupMember = true
      this.$nextTick(() => {
        this.$refs.groupMember.init(id)
      })
    },
    updataList (e) {
      if (e == 1) {
        this.getList()
      }
    },
    // 活动状态
    activeState (row, column) {
      let status = row.active_state
      if (status === 0) {
        return '未开始'
      } else if (status === 1) {
        return '进行中'
      } else {
        return '已结束'
      }
    },
    // 获取列表
    getList () {
      seviceApi.getGroupbuyActivityList(this.pamars).then(res => {
        if (res && res.data.code == 200) {
          this.dataList = res.data.page.list
          this.totalPage = res.data.page.totalCount
        }
      })
    },
    // 每页数
    sizeChangeHandle (val) {
      this.pamars.limit = val
      this.getList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.pamars.page = val
      this.getList()
    },
    // 查询
    getDataList () {
      this.pamars.page = 1
      this.getList()
    },
    // 新增 / 修改
    addOrUpdateHandle (id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.groupBookingAddUpdate.init(id)
      })
    },
    // 拼团管理商品页
    goProduct (id) {
      this.$router.push({ name: 'gb-product', query: { id: id } })
    },

    // 删除
    deleteHandle (id) {
      this.$confirm(`确定要删除该条数据?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        seviceApi.delGroupbuyActivity(id).then(res => {
          if (res && res.data.code == 200) {
            this.$message({
              message: '删除成功',
              type: 'success'
            })
            this.getList()
          }
        })
      }).catch(() => { })
    }
  }
}
</script>
